package com.scs.application.modules.cert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.enums.CertBusType;
import com.scs.application.modules.cert.entity.Certificate;
import com.scs.application.modules.cert.entity.CertificateHospitalBusiness;
import com.scs.application.modules.cert.entity.CertificateRepository;
import com.scs.application.modules.cert.mapper.CertificateMapper;
import com.scs.application.modules.cert.mapper.CertificateRepositoryMapper;
import com.scs.application.modules.cert.mapper.OverallViewerMapper;
import com.scs.application.modules.cert.request.CertificateHospitalBusinessQueryRequest;
import com.scs.application.modules.cert.request.OverallViewerQueryRequest;
import com.scs.application.modules.cert.service.CertificateHospitalBusinessService;
import com.scs.application.modules.cert.service.OverallViewerService;
import com.scs.application.modules.cert.vo.OverAllCatalog;
import com.scs.application.modules.cert.vo.OverAllManufacturer;
import com.scs.application.modules.cert.vo.OverAllMaterial;
import com.scs.application.modules.cert.vo.OverAllSupplier;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName OverallViewSeriveImpl
 * @Description TODO
 */
@Slf4j
@Service
public class OverallViewSeriveImpl implements OverallViewerService {

    @Autowired
    private OverallViewerMapper overallViewerMapper;

    @Autowired
    private CertificateRepositoryMapper certificateRepositoryMapper;

    @Autowired
    private CertificateMapper certificateMapper;

    @Autowired
    private CertificateHospitalBusinessService certificateHospitalBusinessService;

    @Override
    public List<OverAllSupplier> listSupplierCertCnt() {
        return overallViewerMapper.listSupplierCertCnt(UserUtils.getHospitalId());
    }

    @Override
    public List<OverAllManufacturer> listManufacturerCertCnt(String supplierId) {
        return overallViewerMapper.listManufacturerCertCnt(UserUtils.getHospitalId(), supplierId);
    }

    @Override
    public List<OverAllCatalog> listCatalogCertCnt(OverallViewerQueryRequest request) {
        request.setHospitalId(UserUtils.getHospitalId());
        return overallViewerMapper.listCatalogCertCnt(request);
    }

    @Override
    public List<OverAllMaterial> listMaterialCertCnt(OverallViewerQueryRequest request) {
        request.setHospitalId(UserUtils.getHospitalId());
        return overallViewerMapper.listMaterialCertCnt(request);
    }

    @Override
    public IPage<CertificateRepository> pageOverAll(OverallViewerQueryRequest request) {
        request.setHospitalId(UserUtils.getHospitalId());
        return overallViewerMapper.pageOverAll(request.getPage(), request);
    }

    @Override
    public List<CertificateRepository> listForHospital(OverallViewerQueryRequest request) {
        String hospitalId = UserUtils.getHospitalId();
        request.setHospitalId(hospitalId);

        Boolean containParents = request.getContainParents();
        containParents = containParents == null ? false : containParents;

        String businessTypeCode = request.getBusinessTypeCode();

        List<CertificateRepository> list = overallViewerMapper.list(request);

        // 包含所有上级资质
        if (!containParents) {
            return list;
        }

        /** 所有资质列表 */
        List<Certificate> certificates = certificateMapper.selectList(Wrappers.query());
        Map<String, String> certMap = certificates.stream().collect(Collectors.toMap(o -> o.getId(), o -> o.getName(), (o1, o2) -> o1));

        List<String> parentBusTypeCodes = Lists.newArrayList();
        if (CertBusType.MANUFACTURER.getValue().equalsIgnoreCase(businessTypeCode)) {
            parentBusTypeCodes = Lists.newArrayList(CertBusType.SUPPLIER.getValue());
        } else if (CertBusType.CATALOG.getValue().equalsIgnoreCase(businessTypeCode)) {
            parentBusTypeCodes = Lists.newArrayList(CertBusType.SUPPLIER.getValue(), CertBusType.MANUFACTURER.getValue());
        } else if (CertBusType.MATERIAL.getValue().equalsIgnoreCase(businessTypeCode)) {
            parentBusTypeCodes = Lists.newArrayList(CertBusType.SUPPLIER.getValue(), CertBusType.MANUFACTURER.getValue(), CertBusType.CATALOG.getValue());
        }

        CertificateHospitalBusinessQueryRequest queryRequest = new CertificateHospitalBusinessQueryRequest();
        queryRequest.setHospitalId(hospitalId);
        List<CertificateHospitalBusiness> businessList = certificateHospitalBusinessService.list(queryRequest);

        for (String busTypeCode : parentBusTypeCodes) {
            List<CertificateRepository> tmpList = null;

            Set<String> requireCertIds = businessList.stream()
                    .filter(o -> busTypeCode.equalsIgnoreCase(o.getBusinessTypeCode()))
                    .map(o -> o.getCertificateId()).collect(Collectors.toSet());
            if (requireCertIds.isEmpty()) {
                continue;
            }

            QueryWrapper<CertificateRepository> wrapper = Wrappers.<CertificateRepository>query()
                    .eq("business_type_code", busTypeCode)
                    .eq("supplier_id", request.getSupplierId())
                    .eq("close_flag", GlobalConsts.NO_STR)
                    .eq("submit_flag", GlobalConsts.YES_STR)
                    .in("certificate_id", requireCertIds);

            if (CertBusType.SUPPLIER.getValue().equalsIgnoreCase(busTypeCode)) {
                tmpList = certificateRepositoryMapper.selectList(wrapper);

            } else if (CertBusType.MANUFACTURER.getValue().equalsIgnoreCase(busTypeCode)) {
                tmpList = certificateRepositoryMapper.selectList(wrapper
                        .eq("manufacturer_id", request.getManufacturerId()));
            } else if (CertBusType.CATALOG.getValue().equalsIgnoreCase(busTypeCode)) {
                tmpList = certificateRepositoryMapper.selectList(wrapper
                        .eq("manufacturer_id", request.getManufacturerId())
                        .eq("catalog_id", request.getCatalogId()));
            }
            tmpList.forEach(o -> {
                String name = certMap.get(o.getCertificateId());
                o.setCertificateName(name);
            });
            list.addAll(tmpList);

        }

        return list;
    }

    @Override
    public List<OverAllSupplier> listSupplierBySupplier() {
        return overallViewerMapper.listSupplierBySupplier(UserUtils.getSupplierId());
    }

    @Override
    public List<OverAllManufacturer> listManufacturerBySupplier() {
        return overallViewerMapper.listManufacturerForSupplier(UserUtils.getSupplierId());
    }

    @Override
    public List<OverAllCatalog> listCatalogForSupplier(String manufacturerId) {
        return overallViewerMapper.listCatalogForSupplier(UserUtils.getSupplierId(), manufacturerId);
    }

    @Override
    public List<OverAllMaterial> listMaterialForSupplier(String catalogId, String manufacturerId) {
        return overallViewerMapper.listMaterialForSupplier(catalogId, manufacturerId);
    }

    @Override
    public IPage<CertificateRepository> pageOverAllForSupplier(OverallViewerQueryRequest request) {
        if (StringUtils.isBlank(request.getTargetDescribeId())) {
            request.setBusinessTypeCode(CertBusType.SUPPLIER.getValue());
            request.setTargetDescribeId(UserUtils.getSupplierId());
        }
        return overallViewerMapper.pageOverAllForSupplier(request.getPage(), request);
    }


}
